/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-04-03 Marco Riccardi - initial implementation
 *  
 */
package org.mizartools.example.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Calendar;
import java.util.LinkedList;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.mizartools.example.test.FilePrel;
import org.mizartools.system.Article;
import org.mizartools.system.ArticleFactory;
import org.mizartools.system.EnvironFile;
import org.mizartools.system.FileExtension;
import org.mizartools.system.ListArticleFile;

@SuppressWarnings("serial")
public class TestPrelPanel extends JPanel {

	private JTextArea textArea;
	private JScrollPane scroller;
	private JProgressBar progressBar;
	private JButton button;
	private TestFrame testFrame;

	public TestPrelPanel(TestFrame testFrame){
		super();
		this.testFrame = testFrame;
    	setLayout(new BorderLayout());
		setPreferredSize(new Dimension(500, 150));
		button = new JButton();
		AbstractAction action =  new AbstractAction() { 
	 	    public void actionPerformed(ActionEvent e) {
	 	    	textArea.setText(Calendar.getInstance().getTime().toString()+"\n\n");
	 			Job job = new Job();
	 			Thread thread = new Thread(job);
	 			thread.start();
	 	    } 
	 	}; 
	 	button.setAction(action);
	 	button.setMaximumSize(getMaximumSize());
	 	button.setText("Start");
		add(button, BorderLayout.NORTH);
		textArea = new JTextArea();
		textArea.setEditable(false);
        scroller = new JScrollPane();
		scroller.getViewport().add(textArea);
		add(scroller, BorderLayout.CENTER);
		progressBar = new JProgressBar();
		add(progressBar, BorderLayout.SOUTH);
		
	}
	
	private class Job implements Runnable {

		private String prelPath = EnvironFile.getMIZFILES() + File.separator + "prel";
		
		@Override
		public void run() {
			button.setEnabled(false);
			LinkedList<String> articleList = ListArticleFile.getAllArticleList();
	    	progressBar.setMaximum(articleList.size()+1);
	    	progressBar.setValue(0);
	    	progressBar.setIndeterminate(false);
	    	Article article = null;
	    	String articleName = null;
	    	String fileName = null;
	    	String initialChar = null;
	    	String path= null;
	    	int progress = 0;
			FileExtension[] fe = FileExtension.values();
			try {
				String check;
				boolean error = false;
		    	for (String aid : articleList){
		    		if (!error){
			    		article = ArticleFactory.getInstance().getArticle(aid);
			    		articleName = article.getAid().toLowerCase();
						initialChar = articleName.substring(0,1);
						path = prelPath + File.separator + initialChar;
						textArea.append((articleName+"        ").substring(0,10)+" : ");
						for (int i = 0; i < fe.length; i++){
							fileName = article.getAid().toLowerCase() + "."+ fe[i];
							File file = new File(path, fileName);
							if (file.exists()){
								check = FilePrel.check(file);
								textArea.append(fe[i].toString()+" ");
					    		if (!check.equals("")) {
					    			error = true;
									textArea.append("[ERROR] ");
									textArea.append("\n");
									textArea.append(check);
					    		} else textArea.append("[OK] ");
					    		textArea.repaint();
					    		textArea.setCaretPosition(textArea.getText().length()-1);
							}
						}
						textArea.append("\n");
			    		textArea.repaint();
			    		textArea.setCaretPosition(textArea.getText().length()-1);
				    	progressBar.setValue(progress++);
				    	progressBar.repaint();
				    	if (testFrame.isClosed()) {
				    		error = true;
				    	}
		    		}
		    	}
			} 
			catch (Exception e) {
				textArea.append("\n");
	    		textArea.append(fileName);
	    		textArea.append(" -> Error");
				textArea.append("\n");
	    		textArea.append(e.getMessage());
				textArea.append("\n");
	    		textArea.repaint();
	    		textArea.setCaretPosition(textArea.getText().length()-1);
			}
			textArea.append("\n");
			textArea.repaint();
			textArea.setCaretPosition(textArea.getText().length()-1);
	    	progressBar.setValue(0);
			button.setEnabled(true);
		}
		
	}
	
}
